import type { RouteRecordRaw } from 'vue-router'
import { getAllTools } from '@/components/Tools/tools-config'

/**
 * 自动生成工具路由配置
 * 从 tools.ts 中读取工具配置，自动生成路由
 */
// 组件映射，解决Vite动态import的限制
const componentMap: Record<string, any> = {
  'Ip/Ip': () => import('@/components/Tools/Ip/Ip.vue'),
  'JsonTran/JsonTran': () => import('@/components/Tools/JsonTran/JsonTran.vue'),
  'Diff/Diff': () => import('@/components/Tools/Diff/Diff.vue'),
  'TimeTran/TimeTran': () => import('@/components/Tools/TimeTran/TimeTran.vue'),
  'Cron/Cron': () => import('@/components/Tools/Cron/Cron.vue'),
  'RandomPassword/RandomPassword': () => import('@/components/Tools/RandomPassword/RandomPassword.vue'),
  'UrlEncode/UrlEncode': () => import('@/components/Tools/UrlEncode/UrlEncode.vue'),
  'UUID/UUID': () => import('@/components/Tools/UUID/UUID.vue'),
  'MD5/MD5': () => import('@/components/Tools/MD5/MD5.vue'),
  'RegTest/RegTest': () => import('@/components/Tools/RegTest/RegTest.vue'),
  'Unicode/Unicode': () => import('@/components/Tools/Unicode/Unicode.vue'),
  'HttpStatusCode/HttpStatusCode': () => import('@/components/Tools/HttpStatusCode/HttpStatusCode.vue'),
  'JWT/JWT': () => import('@/components/Tools/JWT/JWT.vue'),
  'HtmlEntity/HtmlEntity': () => import('@/components/Tools/HtmlEntity/HtmlEntity.vue'),
  'JSFormat/JSFormat': () => import('@/components/Tools/JSFormat/JSFormat.vue'),
  'HtmlFormat/HtmlFormat': () => import('@/components/Tools/HtmlFormat/HtmlFormat.vue'),
  'CssFormat/CssFormat': () => import('@/components/Tools/CssFormat/CssFormat.vue'),
  'Markdown/Markdown': () => import('@/components/Tools/Markdown/Markdown.vue'),
  'WordCount/WordCount': () => import('@/components/Tools/WordCount/WordCount.vue'),
  'TextRemoveDuplicate/TextRemoveDuplicate': () => import('@/components/Tools/TextRemoveDuplicate/TextRemoveDuplicate.vue'),
  'ASCIIWordPic/ASCIIWordPic': () => import('@/components/Tools/ASCIIWordPic/ASCIIWordPic.vue'),
  'TextEdit/TextEdit': () => import('@/components/Tools/TextEdit/TextEdit.vue'),
  'Unit/Unit': () => import('@/components/Tools/Unit/Unit.vue'),
  'Morse/Morse': () => import('@/components/Tools/Morse/Morse.vue'),
  'ScaleTran/ScaleTran': () => import('@/components/Tools/ScaleTran/ScaleTran.vue'),
  'ASCII/ASCII': () => import('@/components/Tools/ASCII/ASCII.vue'),
  'Qrcode/Qrcode': () => import('@/components/Tools/Qrcode/Qrcode.vue'),
  'SignImage/SignImage': () => import('@/components/Tools/SignImage/SignImage.vue'),
  'TextToImg/TextToImg': () => import('@/components/Tools/TextToImg/TextToImg.vue'),
  'ImgCut/ImgCut': () => import('@/components/Tools/ImgCut/ImgCut.vue'),
  'Chart/Bar/Bar': () => import('@/components/Tools/Chart/Bar/Bar.vue'),
  'Chart/Line/Line': () => import('@/components/Tools/Chart/Line/Line.vue'),
  'Chart/Pie/Pie': () => import('@/components/Tools/Chart/Pie/Pie.vue'),
  'Chart/Scatter/Scatter': () => import('@/components/Tools/Chart/Scatter/Scatter.vue'),
  'Random/Random': () => import('@/components/Tools/Random/Random.vue'),
  'Decision/Decision': () => import('@/components/Tools/Decision/Decision.vue'),
  'Coin/Coin': () => import('@/components/Tools/Coin/Coin.vue'),
  'Dice/Dice': () => import('@/components/Tools/Dice/Dice.vue'),
  'NumberToChinese/NumberToChinese': () => import('@/components/Tools/NumberToChinese/NumberToChinese.vue'),
  'Barrage/Barrage': () => import('@/components/Tools/Barrage/Barrage.vue'),
  'Palettes/Palettes': () => import('@/components/Tools/Palettes/Palettes.vue'),
  'ColorPicker/ColorPicker': () => import('@/components/Tools/ColorPicker/ColorPicker.vue'),
}

export function generateToolRoutes(): RouteRecordRaw[] {
  const tools = getAllTools()
  
  return tools.map(tool => {
    const component = componentMap[tool.component]
    if (!component) {
      console.warn(`Component ${tool.component} not found in componentMap`)
      return null
    }
    
    const route: RouteRecordRaw = {
      path: tool.url,
      component: component,
      name: tool.routeName,
      meta: tool.meta
    }
    return route
  }).filter(Boolean) as RouteRecordRaw[]
}

/**
 * 静态路由配置（首页、404等非工具页面）
 */
export const staticRoutes: RouteRecordRaw[] = [
  // 首页
  {
    path: '/',
    component: () => import('@/components/Home/Home.vue'),
    name: 'home',
    meta: {
      title: '佑佑在线工具箱',
      keywords: '佑佑学习网,在线工具,开发人员工具,时间戳转换,加密,解密,md5,进制转换,二维码,正则表达式,json格式化,照片处理,字数统计',
      description: '佑佑学习网,在线工具,在线工具大全,开发人员工具,日常生活工具,办公助手,时间戳转换,加密,解密,md5,进制转换,二维码,正则表达式,json格式化,照片处理,字数统计',
    }
  },
  // 关于
  {
    path: '/about',
    component: () => import('@/components/Home/About.vue'),
    name: 'about',
    meta: {
      title: "关于",
      keywords: '关于佑佑学习网',
      description: '',
    }
  },
  // 404页面
  {
    path: '/404',
    component: () => import('@/components/404/404.vue'),
    name: '404',
    meta: {
      title: "404"
    }
  },
  // 重定向
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    name: 'Any',
  },
]

/**
 * 合并所有路由
 */
export function getAllRoutes(): RouteRecordRaw[] {
  const toolRoutes = generateToolRoutes()
  return [...staticRoutes, ...toolRoutes]
}